<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: rooms.php,v 1.12 2007/11/08 20:45:24 amorgan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

function getRoomUsers($room_id, $userto) {
	global	$db;
	global	$dbprefix;
	global	$config;

	print("<!-- room id = $room_id, user to = $userto -->\n");
	$rooml	= "<option value=0>Send To Room</option><option value='0'>--------------------</option>\n";
	$sql	= "SELECT * FROM {$dbprefix}sessions AS s,{$dbprefix}prefs AS p WHERE s.user_id=p.user_id AND room_id=$room_id ORDER BY user_alias";
	$res	= $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($res)) {
		$rooml	.= "<option value={$row['user_id']}";
		if ($row['user_id'] == $userto) {
			$rooml	.= " SELECTED";
		}
		$rooml	.= ">{$row['user_alias']}</option>\n";
	}

	return($rooml);
}

function getDynamicGroup() {
	global	$db;
	global	$dbprefix;
	global	$config;

	$sql	= "SELECT * FROM {$dbprefix}room_groups WHERE group_dynamic='yes'";
	$res	= $db->sql_query($sql);
	$row	= $db->sql_fetchrow($res);

	return($row['group_id']);
}

function checkRoomLimit($room) {
	global	$db;
	global	$dbprefix;
	global	$config;

	if ($room['room_limit'] < 1) {
		return 0;
	}

	$sql	= "SELECT COUNT(room_id) AS count FROM {$dbprefix}sessions WHERE room_id={$room['room_id']}";
	if (($result = $db->sql_query($sql)) == NULL) {
		print("DATABASE ERROR!");
		return 1;
	}
	$row	= $db->sql_fetchrow($result);
	if ($row['count'] < $room['room_limit']) {
		return 0;
	}

	return 1;
}

function getRoomList($isMod = false) {
	global	$db;
	global	$dbprefix;
	global	$config;

	if ($config['DEFAULT_ROOM'] == '') {
		$def_room	= 1;
	} else {
		$def_room	= $config['DEFAULT_ROOM'];
	}

	$roomlist	= "<select size=10 name=roomlist class=row1>";
	$sql		= "SELECT
					*
				FROM
					{$dbprefix}rooms AS r,
					{$dbprefix}room_groups AS g
				WHERE
					r.room_group=g.group_id";
	if (!$isMod) {
		$sql		.= "
					AND r.room_dynamic != 'yes'";
	}
	$sql		.= "
				ORDER BY
					room_group, room_name";
				//GROUP BY
					//r.room_id
	print("<!-- SQL: $sql -->\n");
	$count		= 0;
	$room_group	= -1;
	if (($result = $db->sql_query($sql)) == NULL) {
		$str_sql	= ereg_replace("\t", "", $sql);
		$str_sql	= ereg_replace("\n", " ", $str_sql);
		$err		= $db->sql_error($result);
		addToLog("getRoomList: Error #{$err['code']}: {$err['message']} on $str_sql");
		$roomlist	= "DATABASE ERROR";
		return $roomlist;
	}
	while ($row = $db->sql_fetchrow($result)) {
		if ($row['room_group'] != $room_group) {
			$room_group	= $row['room_group'];
			$roomlist	.= "<option value=''>--{$row['group_name']}--</option>\n";
		}
		if ($row['room_id'] == $def_room) {
			$sel	= " selected";
		} else {
			$sel	= "";
		}
		$sql2	= "SELECT COUNT(*) AS count FROM {$dbprefix}sessions WHERE room_id={$row['room_id']}";
		$res	= $db->sql_query($sql2);
		$row2	= $db->sql_fetchrow($res);

		$roomlist	.= "<option value={$row['room_id']} $sel>{$row['room_name']} ({$row2['count']})</option>\n";
	}
	$roomlist	.= "</select>";

	return $roomlist;
}

function getRoomInfo($room_id) {
	global $db;
	global	$dbprefix;
	global	$config;

	$sql	= "SELECT * FROM {$dbprefix}rooms WHERE room_id=$room_id";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);

	return $row;
}

function getRoomNameBySession($sess_id) {
	global $db;
	global	$dbprefix;
	global	$config;

	$sql	= "SELECT room_name FROM {$dbprefix}rooms AS r,{$dbprefix}sessions AS s WHERE s.room_id=r.room_id AND s.session_id='$sess_id'";
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);

	return $row['room_name'];
}

function getRoomInfoByName($room_name) {
	global	$db2;
	global	$dbprefix;

	$sql	= "SELECT * FROM {$dbprefix}rooms WHERE room_name LIKE '$room_name'";
	$result = $db2->sql_query($sql);
	$row	= $db2->sql_fetchrow($result);

	return $row;
}

function getRoomIdByName($room_name) {
	global	$db2;
	global	$dbprefix;

	$sql	= "SELECT * FROM {$dbprefix}rooms WHERE room_name LIKE '$room_name'";
	$result = $db2->sql_query($sql);
	$row	= $db2->sql_fetchrow($result);

	return $row['room_id'];
}
